package com.astro.business;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.astro.domain.bean.TblUserReport;
import com.astro.domain.bean.User;
import com.astro.domain.bean.UserAccount;

@Service("userManager")
@Transactional(value = "transactionManager", propagation = Propagation.REQUIRED)
public class UserManager extends BaseManager {

	
	public User getUserByLoginId(String loginId) throws Exception {

		List<User> customerList = getUserDAO().findByLoginId(loginId);
		return (customerList != null)? customerList.get(0): null;

	}
	
	public User getUserByEmail(String email) throws Exception {
		List<User> customerList = getUserDAO().findByProperty("email", email);
		return (customerList != null)? customerList.get(0): null;

	}
	
	public List<User> listUser() throws Exception {

		return getUserDAO().listUser();

	}
	
	public List<UserAccount> listUserAccount(Long userId) throws Exception {
		return getUserAccountDAO().listUserAccount(userId);
	}

	/**
	 * This method insert data into CUSTOMER table
	 */
	public void addUser(User user) throws Exception {
		if (user != null) {
			
			// MD5 password
			user.setPassword(encodeString(user.getPassword()));
			Date date = new Date();
			user.setCreatedDate(date);
			user.setUpdatedDate(date);
			// Call DAO layer to insert customer into database
			getUserDAO().addUser(user);
		}		
	}
	
	/**
	 * This method insert data into TBL_USER table
	 */
	public void addUserAccount(UserAccount userAccount) throws Exception {
		if (userAccount != null) {
			Date date = new Date();
			userAccount.setCreatedDate(date);
			userAccount.setUpdatedDate(date);
			getUserAccountDAO().addUser(userAccount);
		}		
	}

	/**
	 * This method insert data into TBL_USER_REPORT table
	 */
	public void addUserReport(TblUserReport userReport) throws Exception {
		if (userReport != null) {
			Date date = new Date();
			userReport.setCreatedDate(date);
			userReport.setUpdatedDate(date);
			getUserReportDAO().addUserReport(userReport);
		}		
	}
	
}